﻿using Maticsoft.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace MvcCodeNotes.Models
{
    public class BookCommon
    {
        public static DataTable GetHomepagedataByCache()
        {

            string CacheKey = "homepagedata";
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            DataTable dtAllList = new DataTable();
            if (objModel == null)
            {
                try
                {
                    string strSql = "";
                    Array enumType = Enum.GetValues(typeof(EnumBookCategory));
                    for (int i = 0; i < enumType.Length; i++)
                    {
                        int categoryid = Convert.ToInt32(enumType.GetValue(i));
                        strSql += " select * from (select top 10 Id,BookName,Author,Category from dbo.CW_Book where IsShow=1 and Category=" + categoryid + " order by Sort desc, ClickCount desc ) t" + i;
                        if (i < enumType.Length - 1)
                            strSql += " union all ";
                    }

                    dtAllList = DbHelperSQL.Query(strSql.ToString()).Tables[0];
                    objModel = dtAllList;
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
            else
            {
                dtAllList = (DataTable)objModel;
            }

            return dtAllList;
        }

    }
}